package com.zh.operation.service.impl;
import com.zh.common.utils.StringUtil;
import com.zh.operation.dao.EatDao;
import com.zh.operation.dao.EatPaymentDao;
import com.zh.operation.domain.EatDO;
import com.zh.operation.domain.EatPaymentDO;
import com.zh.operation.service.EatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class EatServiceImpl implements EatService {
	@Autowired
	private EatDao eatDao;
	@Autowired
	private EatPaymentDao paymentDao;
	
	@Override
	public EatDO get(String id){
		return eatDao.get(id);
	}
	
	@Override
	public List<EatDO> list(Map<String, Object> map){
		return eatDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return eatDao.count(map);
	}
	
	@Override
	public int save(EatDO eat){
		eat.setId(StringUtil.generalKeyword());
		eat.setCreateDate(new Date());
		int count = eatDao.save(eat);
		String eatId = eat.getId();//每日菜單ID
		List<String> paymentId=eat.getPaymentIds();//套餐ID
		if(paymentId!=null){
			List<EatPaymentDO> list = new ArrayList<>();//定义一个关联套餐表集合,用来批量增加
			for (String pament:paymentId ){
				EatPaymentDO eatPaymentDO=new EatPaymentDO();
				eatPaymentDO.setId(StringUtil.generalKeyword());
				eatPaymentDO.setEatId(eatId);
				eatPaymentDO.setPaymentId(pament);
				list.add(eatPaymentDO);
			}
			if (list.size() > 0) {
				paymentDao.batchSave(list);
			}
		}
		return count;
	}
	
	@Override
	public int update(EatDO eat){
		paymentDao.remove(eat.getId());//删除数据
		int count = eatDao.update(eat);
		String eatId = eat.getId();//每日菜單ID
		List<String> paymentId=eat.getPaymentIds();//套餐ID
		List<EatPaymentDO> list = new ArrayList<>();//定义一个关联套餐表集合,用来批量增加
		for (String pament:paymentId ){
			EatPaymentDO eatPaymentDO=new EatPaymentDO();
			eatPaymentDO.setId(StringUtil.generalKeyword());
			eatPaymentDO.setEatId(eatId);
			eatPaymentDO.setPaymentId(pament);
			eatPaymentDO.setCreateDate(new Date());
			list.add(eatPaymentDO);
		}
		if (list.size() > 0) {
			paymentDao.batchSave(list);
		}


		return count;
	}
	
	@Override
	public int remove(String id){
		return eatDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return eatDao.batchRemove(ids);
	}

	@Override
	public List<String>  getPaymetName(String id) {
		return eatDao.getPaymetName(id);
	}

	@Override
	public List<String> getPaymetIds(String id) {
		return eatDao.getPaymetIds(id);
	}

}
